//
//  PosterView.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/24.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "PosterView.h"
#import "Header.h"
//#define Kheight  [UIScreen mainScreen].bounds.size.height
//#define kwidth  [UIScreen mainScreen].bounds.size.width
#import "UIViewExt.h"
#import "indexCollectionView.h"
#import "MovieModal.h"


#define kTabBarHeight 49
#define kHeaderViewH 136
#define kIndexCollectionViewH 100
#define kBottomTitleLableH 35
#define kBottomTitleLableY (Kheight-kTabBarHeight-kBottomTitleLableH)
#define kHeaderOffH   36

#define kPosterColletionViewY  (kHeaderViewH-kHeaderOffH)
#define kPosterColletionViewH  (Kheight-kPosterColletionViewY-kTabBarHeight-kBottomTitleLableH)


@implementation PosterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self _creatPostCollectionView];
        
          [self _creatBottomTitleLable];
        
        [self _creatCoverView];
        
        [self _creatHeaderView];
      
        [self _creatSwipeGesTure];
        
        
        //添加观察者
        [self _creatObserver];
    }
    
    return self;
    
}

#pragma mark - 添加观察者
- (void)_creatObserver
{
    
    [_headerView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    
    [_posterCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    
}

#pragma mark - 观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSNumber *number = [change objectForKey:@"new"];
    NSInteger index = [number integerValue];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    //小海报动了
    if ([object isKindOfClass:[indexCollectionView class]] && _posterCollectionView.currentIndex != index)
        
        
    {
        
        
        [_posterCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        
        _posterCollectionView.currentIndex = index;
        
        
        
    }
    else if(  [object isKindOfClass:[PosterCollectionView class]] && _littlePosterView.currentIndex != index)
    {
        
        [_littlePosterView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        
        _littlePosterView.currentIndex = index;
        
        
    }
    
    MovieModal *modal = _movieModalArray[index];
    
    _bottomTitleLable.text = modal.title;
    
    
    
}


- (void)_creatSwipeGesTure
{
    
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)];
    
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self addGestureRecognizer:swipeGesture];
    
    
    
}


- (void)swipeDownAction
{
    NSLog(@"ddd");
    _headerView.top = 64;
    _coverView.hidden = NO;
    UIButton *button = (UIButton *)[_headerView viewWithTag:100];
    button.selected = YES;
}

- (void)_creatCoverView
{
    _coverView = [[UIView alloc]initWithFrame:self.bounds];
    _coverView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
    _coverView.hidden = YES;
    [self addSubview:_coverView];
    
    UIControl *singleTap = [[UIControl alloc]initWithFrame:_coverView.bounds];
    [singleTap addTarget:self action:@selector(singleTapAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_coverView addSubview:singleTap];
    
}

- (void)singleTapAction
{
    
    _headerView.top = -kHeaderOffH;
    _coverView.hidden = YES;
    UIButton *button = (UIButton *)[_headerView viewWithTag:100];
    button.selected = NO;
}

#pragma mark -创建标题栏
- (void)_creatHeaderView
{
    
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, -kHeaderOffH, kwidth, kHeaderViewH)];
    _headerView.backgroundColor = [UIColor cyanColor];
    [self addSubview:_headerView];
    
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:_headerView.bounds];
    bgImageView.image = image;
    [_headerView addSubview:bgImageView];
    
    
    //创建小海报
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(kwidth/5, kIndexCollectionViewH);
    
    //设置collectionView的滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _littlePosterView = [[indexCollectionView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kIndexCollectionViewH) collectionViewLayout:layout];
    
    _littlePosterView.backgroundColor = [UIColor grayColor];
    
    
    [_headerView addSubview:_littlePosterView];
    
    
    
    
    
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((kwidth-20)/2, (kHeaderViewH-25), 20, 20)];
//    btn.backgroundColor = [UIColor orangeColor];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setImage:[UIImage imageNamed:@"down_home@2x"] forState:UIControlStateNormal];
    [btn  setImage:[UIImage imageNamed:@"up_home@2x"] forState:UIControlStateSelected];
    btn.selected = NO;
    btn.tag = 100;
    [_headerView addSubview:btn];
    
    
}

- (void)btnAction:(UIButton *)btn
{
    [UIView beginAnimations:nil context:nil];
    
    [UIView animateWithDuration:1.0 animations:nil];
    
    if (!btn.selected) {
        
           _headerView.top = 64;
        _coverView.hidden = NO;
        
    }
    else
    {
        _headerView.top = -kHeaderOffH;
        _coverView.hidden = YES;
        
        
    }
    
    btn.selected = !btn.selected;
    
    [UIView commitAnimations];
    
}

//- (void)setImageUrlArray:(NSArray *)imageUrlArray
//{
//    _imageUrlArray = imageUrlArray;
//    
//    
//    _posterCollectionView.imageUrlArray = _imageUrlArray;
//    
//}

- (void)setMovieModalArray:(NSArray *)movieModalArray
{
    _movieModalArray = movieModalArray;
    
    _posterCollectionView.movieModalArray = _movieModalArray;
    
    _littlePosterView.movieModalArray = _movieModalArray;
    

}

- (void)_creatPostCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    layout.itemSize
    
    
    //itemSizes设置的是cell的大小，
    layout.itemSize = CGSizeMake(kwidth*3/4, kPosterColletionViewH);
    
    //默认是有间距的
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _posterCollectionView = [[PosterCollectionView alloc]initWithFrame:CGRectMake(0, kPosterColletionViewY, kwidth, kPosterColletionViewH) collectionViewLayout:layout];
    
    
    _posterCollectionView.backgroundColor = [UIColor grayColor];
    
    _posterCollectionView.backgroundColor = [UIColor purpleColor];
    
    
    
    [self addSubview:_posterCollectionView];
    
    
}


- (void)_creatBottomTitleLable
{
    
    _bottomTitleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, kBottomTitleLableY, kwidth, kBottomTitleLableH)];
    _bottomTitleLable.textColor = [UIColor orangeColor];
    _bottomTitleLable.backgroundColor = [UIColor blackColor];
    _bottomTitleLable.textAlignment = NSTextAlignmentCenter;

    
    [self addSubview:_bottomTitleLable];
    
}
@end
